<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\BaseController;
use think\Request;

class ApiController extends BaseController
{
  /*
   * 获取客户列表数据
   */
  public function getCustomer(Request $request)
  {
    $page = $request->param('page', '');
    $limit = $request->param('limit', '');
    $data = [
      'page' => $page,
      'limit' => $limit
    ];
    $url = 'http://huiyicrm.prowealthgroup.cn/debug.php?s=/Debug/getCustomer';
    $res = self::curlFunction($url, 'post', $data);
    if ($res) {
      $resJsonDecode = json_decode($res,true);
      $resultData = [];
      if ($resJsonDecode['data']) {
        foreach ($resJsonDecode['data'] as $v) {
          if (!isset($resultData[$v['company']])) {
            $resultData[$v['company']] = $v;
          }
        }
      }
      return $this->send200Response(array_values($resultData));
    } else {
      return $this->send400Response('调用接口失败');
    }
  }
  public function curlFunction($url, $method='get', $data=[]) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if ($method == 'post') {
      curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    }
    $res = curl_exec($ch);
    curl_close($ch);
    if ($res)
      return $res;
    return false;
  }
}
